<?php
namespace Common\Model;

class TagsModel extends AiModel {

	public function getAllTags() {
		$result = $this->order('id ASC')->select();

		foreach ($result as $k => $v) {
			$result[$k]['html'] = '<span id="tags_id_'.$v['id'].'" style="margin-top:15px; margin-right:10px; font-size:15px; padding:5px 10px; background-color:'.get_color().'; color:white;">'.$v['name'].'<a onclick="delTags(\''.$v['id'].'\')" href="javascript:void(0);" style="margin-left:15px; color:#b63738; cursor:pointer;">x</a></span>';
		}

		return $result;
	}

	public function getOneTags($name, $id) {
		return '<span id="tags_id_'.$id.'" style="margin-top:15px; margin-right:10px; font-size:15px; padding:5px 10px; background-color:'.get_color().'; color:white;">'.$name.'<a onclick="delTags(\''.$id.'\')" href="javascript:void(0);" style="margin-left:15px; color:#b63738; cursor:pointer;">x</a></span>';
	}

	public function getTagsList($c = false) {
		if ($c) {
			S('tags_list', null);
		}

		$result = S('tags_list');

		if ($result == '') {
			$result = $this->field('id, name')->select();

			$list = array();
			foreach ($result as $k => $v) {
				$list[] = array(
					'id'	=> $v['id'],
					'name' 	=> $v['name'],
					'color'	=> get_color()
				);
			}

			S('tags_list', $list);
			return $list;
		}

		return $result;
	}

	public function getWeekAllTags($day = 0) {
		if ($day == 0) return false;

		$ids = D('Knowledge')->getWeekAllKnowledgeId($day);
		if ($ids == '') return '';

 		$sql = "SELECT tags FROM jj_knowledge WHERE id IN (".implode(',', $ids).")";
		$result = M()->query($sql);
		if ($result === null) return '';

		$result = dimensionality_reduction($result, 'tags');

		$tags_str = '';
		foreach ($result as $v) {
			$tags_str .= $v.',';
		}
		$tags_str = trim($tags_str, ',');

		$arr = explode(',', $tags_str);
		$new_arr = array_rand(array_unique($arr), 8);

		$new_list = array();
		foreach ($new_arr as $v) {
			$new_list[] = array(
				'tags' 	=> $arr[$v],
				'color'	=> get_color(),
				'style'	=> 'style="background: '.get_color().'"',
			);
		}

		return $new_list;
	}

}